<!--  -->
<template>
  <div class="meedu-main-body">
    <BackBar class="mb-30" title="课时评论"></BackBar>
    <div class="float-left j-b-flex mb-30">
      <div class="d-flex">
        <el-button type="danger" @click="delRecords()">删除</el-button>
      </div>
      <div class="d-flex">
        <div>
          <el-input
            class="w-150px"
            v-model="filter.user_id"
            placeholder="学员ID"
          ></el-input>
        </div>
        <div class="ml-10">
          <el-select
            filterable
            :filter-method="dataFilter"
            placeholder="课时"
            class="w-150px"
            v-model="filter.video_id"
            v-el-select-loadmore="loadmore"
          >
            <el-option
              v-for="(item, index) in filterData.videos"
              :key="index"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
        <div class="ml-10">
          <el-date-picker
            :picker-options="pickerOptions"
            v-model="filter.created_at"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="评论时间-开始"
            end-placeholder="评论时间-结束"
          >
          </el-date-picker>
        </div>
        <div class="ml-10">
          <el-button @click="paginationReset()">清空</el-button>
          <el-button @click="firstPageLoad()" type="primary"> 筛选 </el-button>
        </div>
      </div>
    </div>
    <div class="float-left">
      <div class="float-left">
        <el-table
          :header-cell-style="{ background: '#f1f2f9' }"
          :data="list"
          class="float-left"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="user_id" label="学员ID" width="120">
          </el-table-column>
          <el-table-column label="学员" width="300">
            <template slot-scope="scope">
              <div class="user-item d-flex" v-if="users[scope.row.user_id]">
                <div class="avatar">
                  <img
                    :src="users[scope.row.user_id].avatar"
                    width="40"
                    height="40"
                  />
                </div>
                <div class="ml-10">
                  {{ users[scope.row.user_id].nick_name }}
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="video.title" label="课时"> </el-table-column>
          <el-table-column label="内容">
            <template slot-scope="scope">
              <div v-html="scope.row.render_content"></div>
            </template>
          </el-table-column>
          <el-table-column label="时间" width="200">
            <template slot-scope="scope">{{
              scope.row.created_at | dateFormat
            }}</template>
          </el-table-column>
        </el-table>
        <div class="float-left text-center mt-30">
          <el-pagination
            @size-change="paginationSizeChange"
            @current-change="paginationPageChange"
            :current-page="pagination.page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pagination.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import BackBar from "@/components/back-bar.vue";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: { BackBar },
  directives: {
    "el-select-loadmore": {
      bind(el, binding) {
        const SELECTWRAP_DOM = el.querySelector(
          ".el-select-dropdown .el-select-dropdown__wrap"
        );
        SELECTWRAP_DOM.addEventListener("scroll", function () {
          const condition =
            this.scrollHeight - this.scrollTop <= this.clientHeight;
          if (condition) {
            binding.value();
          }
        });
      },
    },
  },
  data() {
    //这里存放数据
    return {
      pagination: {
        page: 1,
        size: 10,
      },
      filter: {
        video_id: null,
        user_id: null,
        created_at: null,
      },
      filterData: {
        videos: [],
      },
      formData: {
        keywords: null,
        page: 1,
        size: 10,
      },
      total: 0,
      loading: false,
      users: [],
      list: [],
      selectedRows: null,
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
      is_all: false,
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    getData() {
      if (this.loading) return;
      this.loading = true;
      let params = { course_id: this.id };
      Object.assign(params, this.pagination, this.filter);
      this.$request.get(`video_comment`, { params }).then((res) => {
        this.loading = false;
        this.list = res.data.data.data;
        this.total = res.data.data.total;
        this.users = res.data.users;
      });
    },
    params() {
      this.$request.get(`video`, { params: this.formData }).then((res) => {
        if (res.data.videos.data.length === 0) this.is_all = true;
        this.filterData.videos = [
          ...this.filterData.videos,
          ...res.data.videos.data,
        ];
      });
    },
    loadmore() {
      if (this.is_all) return;
      this.formData.page++;
      this.params();
    },
    dataFilter(val) {
      this.is_all = false;
      this.formData.keywords = val;
      this.formData.page = 1;
      this.$request.get(`video`, { params: this.formData }).then((res) => {
        this.filterData.videos = res.data.videos.data;
      });
    },
    paginationReset() {
      this.pagination.page = 1;
      this.filter.video_id = null;
      this.filter.user_id = null;
      this.filter.created_at = null;
      this.formData.keywords = null;
      this.is_all = false;
      this.params();
      this.getData();
    },
    firstPageLoad() {
      this.pagination.page = 1;
      this.getData();
    },
    handleSelectionChange(rows) {
      this.selectedRows = rows;
    },
    paginationSizeChange(size) {
      this.pagination.size = size;
      this.getData();
    },
    paginationPageChange(page) {
      this.pagination.page = page;
      this.getData();
    },
    delRecords() {
      if (this.loading) {
        return;
      }
      if (this.selectedRows === null) {
        this.$message.warning("请选择需要操作的数据");
        return;
      }
      this.$confirm("确认操作？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.loading = true;

          let ids = [];
          this.selectedRows.forEach((item) => {
            ids.push(item.id);
          });

          this.$request
            .post(`video_comment/delete`, { ids: ids })
            .then(() => {
              this.loading = false;
              this.$message.success(this.$t("common.success"));
              this.firstPageLoad();
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        })
        .catch(() => {
          //点击删除按钮的操作
        });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getData();
    this.params();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='less' scoped>
</style>